//
//  PKDownLoadManager.m
//  PianKe
//
//  Created by ybq on 16/1/5.
//  Copyright © 2016年 B7_Young. All rights reserved.
//

#import "PKDownLoadManager.h"

@implementation PKDownLoadManager

- (instancetype)init {
    if (self = [super init]) {
        _downLoadArray = [NSMutableArray array];
        
    }
    return self;
}

+ (instancetype)sharedManager {
    static PKDownLoadManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PKDownLoadManager alloc] init];
    });
    return manager;
}

- (void)addDownLoadToManager:(PKDownLoad *)downLoad {
    // 添加下载任务到下载管理器,用一个数组保存
    [_downLoadArray addObject:downLoad];
}

- (void)removeDownLoad:(PKDownLoad *)downLoad {
    // 移除任务,把下载任务从数组中清除
    [_downLoadArray removeObject:downLoad];
}

- (PKDownLoad *)downLoadFrom:(PKRadioDetailModel *)model {
    // 根据model判断这个model对应的歌曲是否正在下载
    // model里面的属性musicUrl可以作为判断的依据
    NSString *url = model.musicUrl;
    PKDownLoad *load = nil;
    for (PKDownLoad *downLoad in _downLoadArray) {
        // 判断model的url是否正在被下载
        if ([url isEqualToString:downLoad.model.musicUrl]) {
            // 如果正在下载就返回这个downLoad,否则返回nil
            load = downLoad;
        }
    }
    return load;
}

@end
